/**
 * 
 */
package com.jm.web.form.baseitem;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.data.form.Item;
import com.jm.web.form.support.FieldItem;

/**
 * @author yanjie
 *
 * 2011/10/7 上午8:46:22
 */
public class CheckboxItem extends RepeatListItem{
	/**
	 * @param tag
	 */
	public CheckboxItem() {}
	
	@Override
	public void init() {
		super.init();
		this.setNodeName(FieldItem.TAG_ITEMNAME);
	}
	
	@Override
	public void renderBegin(JspWriter writer) {
		if(this.getBaseTag() != null && this.getBaseTag().isDSP())
			super.renderBegin(writer);
	}
	
	
	@Override
	public void renderEnd(JspWriter writer) {
		if(this.getBaseTag() != null && this.getBaseTag().isDSP())
			super.renderEnd(writer);
	}
	
	@Override
	public void OutputSubItem(JspWriter writer,int index, Item listItem) {
		AttributeBuilder attributesBuilder = new AttributeBuilder();
		attributesBuilder.InitFromHashtable(this.extAttrList,true);
		FillAttributeBuilder(attributesBuilder);
		attributesBuilder.Remove("id");
		attributesBuilder.Remove("ID");
		attributesBuilder.Remove("required");
		attributesBuilder.Remove("rule");
		attributesBuilder.Remove("msg");
		attributesBuilder.Remove("disabled");
		attributesBuilder.Remove("WITHHIDDEN");
		attributesBuilder.Set("id",StringHelper.Format("%1$s%2$s",this.getId(),index));
		
		StringBuilderEx sb = new StringBuilderEx();
		boolean bchecked = this.getValue().equalsIgnoreCase(listItem.getValue());
		if(this.IsContainsKey("checked")){
			bchecked = "checked".equals(this.GetExtValue("checked","")) || "true".equals(this.GetExtValue("checked",""));
		}
		
		if(!bchecked && !this.valuesIsEmpty() && this.getValues().contains(listItem.getValue())){
			bchecked = true;
		}
		
		
		if(this.getBaseTag().GetExtValue("withHidden",false)){
			sb.Append("<input type=\"hidden\" value=\"%1$s\" index=\"%2$s\" %3$s %4$s ",
					bchecked ? listItem.getValue() : "",
					StringHelper.getValue(index),
					listItem.getExtToString(),
					attributesBuilder.ToOutputString());
			sb.Append("/>");
			
			//this.getBaseTag().getPage().getTagItemSize(this.getName())-1
			
			sb.Append("<input type=\"checkbox\" value=\"%1$s\" index=\"%2$s\" dname=\"%3$s\" name=\"%3$s_checkbox\" oncheck=\"$P.checkboxChang(this);\" onclick=\"$P.checkboxChang(this);%4$s;\" ",
					listItem.getValue(),
					StringHelper.getValue(index),
					this.getName(),
					this.GetExtValue("onclick","")
					);
			
			if(bchecked)
				sb.Append(" checked=\"true\" ");
			
			if(this.isDisabled()){
				sb.Append(" disabled=\"true\" ");
			}
			
			if(!StringHelper.IsNullOrEmpty(this.getStyle())){
				sb.Append(" style=\"%1$s\" ",this.getStyle());
			}
			
			sb.Append("/>");
		}else{
			sb.Append("<input type=\"checkbox\" value=\"%1$s\" index=\"%2$s\" %3$s %4$s ",
					listItem.getValue(),
					StringHelper.getValue(index),
					listItem.getExtToString(),
					attributesBuilder.ToOutputString());
			
			if(bchecked)
				sb.Append("checked=\"true\" ");
			
			if(!StringHelper.IsNullOrEmpty(this.getStyle())){
				sb.Append(" style=\"%1$s\" ",this.getStyle());
			}
			
			sb.Append("/>");
			if(!StringHelper.IsNullOrEmpty(listItem.getText())){
				sb.Append("<span class=\"sx-buttoncaption \"  >%1$s</span>",listItem.getText(),!this.isEnab() ? "ax-disabled" : "");
			}
		}
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
